<?php
class OrderCompleteAction extends SzAbstractAction
{
    protected $paramTypes = array(
        SzAbstractAction::TYPE_INT,
        SzAbstractAction::TYPE_INT,
        SzAbstractAction::TYPE_INT,
        SzAbstractAction::TYPE_INT,
    );

    /**
     * 完成一个订单
     * @param $userId
     * @param $position
     * @param $orderId
     * @param $reqTime
     * @return array|SzResponse
     * @throws SzException
     */
    public function execute($userId,  $position, $orderId, $reqTime)
    {
        $res = array(
            'code' => 0,
            'data' => array()
        );

        GameService::get()->validateMultiRequestTimeDelta($userId, $reqTime);
        if (!GameService::get()->verifySysUnlock($userId, GameConst::FUNC_NORMAL_ORDER)) {
            return $this->buildResponse(false);
        }
        $completeNum=1;
        $orderArr=array();

        $flowerOrderLimit = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'flowerOrderLimit', true,false);
        if (!$flowerOrderLimit) {
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }else{
            $flowerOrderLimit = $flowerOrderLimit['value'];
        }
        //获取玩家当天完成的订单数
        $Ymd=date('Ymd',$reqTime);
        $dateInt=strtotime($Ymd);
        /**
         * @var $userShopList UserShopVoList
         * @var $userShop  UserShopVo
         */
        $userShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_SHOP);
        $userShop = $userShopList->getElement($dateInt,false);
        if ($userShop){
            $completeNum = $userShop->getFlowerOrder()+1;
            if ($completeNum>$flowerOrderLimit){
                $orderArr['completeNum']=$completeNum;
                return $this->buildResponse(
                    $orderArr
                );
            }
        }
        $orderTemp = OrderService::get()->complete($userId, $position, $orderId, $reqTime);
        if ($orderTemp['code'] != 0){
            $res['code'] = $orderTemp['code'];
            return $res;
        }
        $orderArr['order']= $orderTemp['order'];
        $orderArr['completeNum']=$completeNum;

        $res['data'] = $orderArr;
        return $this->buildResponse($res);
    }
}